package com.example.okhttpdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.okhttpdemo.api.Api;
import com.example.okhttpdemo.api.ApiConfig;
import com.example.okhttpdemo.api.ResponseCallback;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView txtContent;
    private Button btnClick;
    private Button btnPostClick;

    private Handler handler = new Handler(Looper.myLooper()) {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                String result = (String) msg.obj;

                txtContent.setText(result);
                Toast.makeText(MainActivity.this, "主线程任务完成", Toast.LENGTH_SHORT).show();
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtContent = findViewById(R.id.txtContent);
        btnClick = findViewById(R.id.btnClick);
        btnPostClick=findViewById(R.id.btnPostClick);

        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });



        btnPostClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postLoginNet();
            }
        });


    }

    public void start() {
        getStringFormatNet();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                String result = getStringFormatNet();
//                Message message = Message.obtain();
//                message.what = 0;
//                message.obj = result;
//                handler.sendMessage(message);
//
//            }
//        }).start();
        Toast.makeText(this, "任务完成", Toast.LENGTH_SHORT).show();
    }

    private void getStringFormatNet() {
//        final String result = "测试";
//        String url = "http://wwww.baidu.com";
//        OkHttpClient okHttpClient = new OkHttpClient();
//        final Request request = new Request.Builder()
//                .url(url)
//                .build();
//        final Call call = okHttpClient.newCall(request);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Response response = call.execute();
//
//                    Message message = Message.obtain();
//                    message.what = 0;
//                    message.obj = response.body().string();
//                    handler.sendMessage(message);
//
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();

//
//        String url = "http://192.168.1.106:7001/api/articlecat/getArticleCatWithIdAndCatName";
//        OkHttpClient okHttpClient = new OkHttpClient();
//        final Request request = new Request.Builder()
//                .url(url)
//                .get()//默认就是GET请求，可以不写
//                .build();
//        Call call = okHttpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.d("问题", "onFailure: ");
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//                Message message = Message.obtain();
//                message.what = 0;
//                message.obj = response.body().string();
//                handler.sendMessage(message);
//            }
//        });

        Api.config("api/articlecate/getmobilearticlecates",null).getAsync(new ResponseCallback() {
            @Override
            public void OnSuccess(String res) {
                Message message = Message.obtain();
                message.what = 0;
                message.obj = (res==null ? "no data" : res);
                handler.sendMessage(message);

            }

            @Override
            public void OnFailure(Exception e) {
                Message message = Message.obtain();
                message.what = 0;
                message.obj = e.getMessage();
                handler.sendMessage(message);
            }
        });

    }
    private void postLoginNet(){

        Map<String,Object> mParams=new HashMap<>();
        mParams.put("email","1936979980@qq.com");
        mParams.put("password","123456");
        Api.config(ApiConfig.LOGIN,mParams).postAsyc(new ResponseCallback() {
            @Override
            public void OnSuccess(String res) {
                Message message = Message.obtain();
                message.what = 0;
                message.obj = res;
                handler.sendMessage(message);
            }

            @Override
            public void OnFailure(Exception e) {
                Message message = Message.obtain();
                message.what = 0;
                message.obj = e.getMessage();
                handler.sendMessage(message);
            }
        });

    }
}